package com.mkvld.appietijdenv2.activities;

import com.mkvld.appietijdenv2.R;
import com.mkvld.appietijdenv2.data.Payment;
import com.mkvld.appietijdenv2.data.PeriodBudget;
import com.mkvld.appietijdenv2.data.PeriodShifts;
import com.mkvld.appietijdenv2.data.Preferences;
import com.mkvld.appietijdenv2.data.Utils;
import com.mkvld.appietijdenv2.databases.ShiftDatabase;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.Menu;
import android.view.MenuItem;

public abstract class ActMainV9 extends ActV9 {
	int period, year;
	public static PeriodShifts periodShifts = null;
	Payment payment;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		period = Utils.getCurrentPeriod();
		year = Utils.getCurrentYear();
		if (Preferences.getContract(getApplicationContext()) > 48)
			PeriodBudget.initPeriodBudgetList(getApplicationContext());
		periodShifts = ShiftDatabase.db.getPeriodShifts(year, period);
		
		LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(new BroadcastReceiver() {
			
			@Override
			public void onReceive(Context context, Intent intent) {
				update();
			}
		}, new IntentFilter("appietijden.update"));
		
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.am_menu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.amm_prev) {
			prevPeriod();
			return true;
		} else if (item.getItemId() == R.id.amm_next) {
			nextPeriod();
			return true;
		}
		return false;
	}
	
	public void nextPeriod() {
		if (++period > 12) {
			period = 0;
			year++;
		}
		update();
	}
	
	public void prevPeriod() {
		if (--period < 0) {
			period = 12;
			year--;
		}
		update();
	}
	
	public int getPeriod() {
		return period;
	}
	
	public int getYear() {
		return year;
	}
	
	@Override
	public void update() {
		periodShifts = ShiftDatabase.db.getPeriodShifts(year, period);
	}
}
